package com.ityunhe.sevlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ityunhe.bean.Garden;
import com.ityunhe.biz.GardenAccountBis;

/**
 * 菜农信息
 * @author Administrator
 *
 */
@WebServlet("/home/GardenAccountServlet")
public class GardenAccountServlet extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("GardenAccountServlet");
		GardenAccountBis gardenAccountBis = new GardenAccountBis();
		//获取数据
		String opr = request.getParameter("opr");
		if("regist".equals(opr)){
			String gardenname = request.getParameter("username");
			String gardenpwd = request.getParameter("userpwd");
			String gardenmobile = request.getParameter("usermobile");
			//创建对象
			Garden garden = new Garden();
			garden.setGardenname(gardenname);
			garden.setGardenpassword(gardenpwd);
			garden.setGardenmobile(gardenmobile);
			
			int result=0;
			try {
				result = gardenAccountBis.registGarden(garden);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(result>0){
				//注册成功,
				request.setAttribute("garden",garden);
				//id为2代表是菜农
				request.getRequestDispatcher("/home/login.jsp?id=2").forward(request, response);	
			}else{
				//注册失败
				request.setAttribute("error", "注册失败");
				request.getRequestDispatcher("/home/register.jsp").forward(request, response);	
			}
		}
		
		
		
		
	}

}
